namespace EFS.Common.Functions
{
	public static class Address
	{
		/// <summary>
		/// return PostalCode with or without space char inside
		/// </summary>
		/// <param name="postalCode"></param>
		/// <param name="withSpaceReturn"></param>
		/// <returns></returns>
		public static System.String PostalCodeFormat(System.String postalCode, System.Boolean withSpaceReturn)
		{
			// PostalCode format:
			// with space:		'EX1 3DF'
			// without space:	'EX13DF'

			System.String newPC;
			System.Int32 spacePosition = 3;

			if (null == postalCode)
			{
				return "";
			}

			// remove spaces
			newPC = postalCode.Replace(" ", "");

			if (withSpaceReturn && (newPC.Length > spacePosition))
			{
				// add space
				newPC = newPC.Substring(0, spacePosition) + " " + newPC.Substring(spacePosition, newPC.Length - spacePosition);
			}

			return newPC;
		}

		/// <summary>
		/// check postalcode
		/// </summary>
		/// <param name="postalCode"></param>
		/// <param name="checkType"></param>
		/// <param name="errorMsg"></param>
		/// <returns></returns>
		public static System.Boolean PostalCodeCheck(System.String postalCode, System.String checkType, out System.String errorMsg)
		{
			System.Boolean ret = true;
			System.String error=null;

			if (null == postalCode)
			{
				errorMsg = "Postcode not supplied";
				return false;
			}

			switch (checkType)
			{
				case "length_uk":
					{
						if (postalCode.Length < 5 || postalCode.Length > 7)
						{
							error = "Postcode length must be between 5 and 7 characters";
							ret = false;
						}
						break;
					}
				default:
					{
						error = "Validation '"+checkType+"' not defined";
						ret = false;
						break;
					}
			}

			errorMsg = error;
			return ret;
		}
	}
}
